var unlikePlace = function(placeId,accountId){
    this.placeId = placeId;
    this.accountId = accountId;
    this.error = options.error;
    this.success = options.success;
    this.unlikePlace();
};

unlikePlace.prototype._success = function(data){
    if(typeof this.success == 'function') {
        this.success(data);
    }
};

unlikePlace.prototype._error = function(code, message){
    if(typeof this.error == 'function') {
        this.error(code, message);
    }
};

unlikePlace.prototype.check = function(nextStatement){
    var self = this;
    Mysql.query("SELECT COUNT(*) AS c1 FROM place WHERE id="+Mysql.escape(self.placeId)+" LIMIT 1", function(error, results){
        if(error) {
            console.log('Mysql error: '+error);
            self._error(9999, 'Mysql error');
            return;
        }
        if(results[0].c1 == 0) {
            self._error(9999, 'Not found account');
            return;
        }
        nextStatement();
    });
};

unlikePlace.prototype.unlikePlace = function(){
    var self = this;
    self.check(function(){
        Mysql.query("SELECT id,likeGroupId FROM place WHERE id="+Mysql.escape(self.placeId), function(error, results){
            if(error) {
                console.log('Mysql error: '+error);
                self._error(9999, 'Mysql error');
                return;
            }
            Mysql.query("DELETE FROM likegroup WHERE id = "+results.likeGroupId, function(error, results){
                if(error) {
                    console.log('Mysql error: '+error);
                    self._error(9999, 'Mysql error');
                    return;
                }
                Mysql.query("DELETE FROM like WHERE id = "+results.id, function(error, results){
                    if(error) {
                        console.log('Mysql error: '+error);
                        self._error(9999, 'Mysql error');
                        return;
                    }
                    self._success({
                        id: results.id, 
                        name: results.ownerId, 
                        photoGroup: results.likeGroupId
                    });
                });
            });
        });
        
    });
};

module.exports = unlikePlace;